package ftp;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
	
	public static void main(String[] args) throws Exception, IOException {
		
		Socket socket = new Socket("127.0.0.1",8888);
		BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		OutputStream out = socket.getOutputStream();
		BufferedReader readerInConsole = new BufferedReader(new InputStreamReader(System.in));
		
		while(true){
			String line = reader.readLine();
			System.out.println(line);
			
//			if(line.startsWith("hello")){
//				line = reader.readLine();
//			}
//			
			if(line.startsWith("file")){
				line = line.replace("file","");
				byte[] b = line.getBytes();
				
				OutputStream newOut = new FileOutputStream("1.txt");
				BufferedOutputStream bout = new BufferedOutputStream(newOut,64);
				bout.write(b);
			}



			
			if(line.startsWith("file")){
				line = line.replace("file","");
				byte[] b = line.getBytes();
				
				OutputStream newOut = new FileOutputStream("1.txt");
				BufferedOutputStream bout = new BufferedOutputStream(newOut,64);
				bout.write(b);
			}

			String command = readerInConsole.readLine();
			if(command.equals("quit")){
				out.write("bye\n".getBytes());
				break;
			}
			
			out.write((command + "\n").getBytes());
		}
		reader.close();
		out.close();
		socket.close();
	}
}
